#include "threadPool.h"
int transfile(int netfd,char *filename){
    int fd=open(filename,O_RDWR);
    NETDISK_LOG_DEBUG
    
    train_t train;
    bzero(&train,sizeof(train));
    
    struct stat statbuf;
    fstat(fd,&statbuf);
    ssize_t fileLength=0;
    
    recv(netfd,&fileLength,sizeof(ssize_t),MSG_NOSIGNAL);
    //收到要下载的文件当前长度
    statbuf.st_size -= fileLength;
    if(fileLength!=0){
        lseek(fd,fileLength-1,SEEK_SET);   
    }
    //移动到当前长度的偏移量
    
    train.length=statbuf.st_size;
    //发送了一个fd的剩余的总长度  
    send(netfd,&train.length,sizeof(int),MSG_NOSIGNAL);
    
    if(statbuf.st_size<100000000){
        while(1){
            bzero(&train,sizeof(train));
            ssize_t ret=read(fd,train.data,sizeof(train.data));
            train.length=ret;
            send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
            if(ret==0){
                break;
            }
        }
    }else{
        char*p = (char*)mmap(NULL,statbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
        p+=fileLength;
        off_t cursize=0;
        while(cursize<statbuf.st_size){
            if(statbuf.st_size-cursize>1000){
                train.length=1000;
            }else{
                train.length=statbuf.st_size-cursize;      
            }
            send(netfd,&train.length,sizeof(int),MSG_NOSIGNAL);
            send(netfd,p+cursize,train.length,MSG_NOSIGNAL);
            cursize+=train.length;
        }
        printf("传输完成\n");
        train.length=0;
        send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
        munmap(p,statbuf.st_size);
    }
    close(fd);
    return 0;
}

int recvn(int netfd, void *buf, int length){
    int total = 0;
    char *p = (char *)buf;
    while(total < length){
        int ret = recv(netfd,p+total,length-total,0);
        if(ret == 0){
            NETDISK_LOG_DEBUG
            return -1;
        }
        total += ret;
    }
    return 0;
}

int getsfile(int netfd,char* filename){

    int length;
    
    int fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,0666);
    off_t filesize;
    recvn(netfd,&length,sizeof(int));
    recvn(netfd,&filesize,length);
    //printf("filesize = %ld\n",filesize);
    off_t cursize = 0;
    off_t slice = 0;
    off_t lastsize = 0;
    char buf[1024]={0};
    while(1){
        length=0;
        recvn(netfd,&length,sizeof(int));
        //if(length!=1000){
        //    printf("length = %d\n",length);           
        //}
        if(length==0){
            break;
        }
        bzero(buf,sizeof(buf));
        recvn(netfd,buf,length);
        cursize +=length;
        if(cursize-lastsize>slice){
            printf("%5.2lf%%\r",100.0*cursize / filesize);
            fflush(stdout);
            //刷新流缓冲区
            lastsize=cursize;
        }
        write(fd,buf,length);
    }      
    printf("100.00%%\n");
    close(fd);
    return 0;
}
int recv_msg(int netfd,char *buf)
  {
      int length;
      recvn(netfd,&length,sizeof(int));
      recvn(netfd,buf,length);
      return 0;
  }

int send_msg(int netfd,char *buf)
  {
      int length;
      length = strlen(buf);
      send(netfd,&length,sizeof(int),0);
      send(netfd,buf,length,0);
      return 0;
  }




